<?php


namespace app\services\area;


use app\common\CacheKey;
use app\models\region\RegionModel;
use app\services\BaseService;
use think\facade\Cache;
use think\facade\Db;

class RegionService extends BaseService
{
    protected static function initModel()
    {
        return new RegionModel();
    }

    /**
     * 获取省ID
     * @return array|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getProvinceListService()
    {
        $list = Cache::get(CacheKey::PROVINCE_LIST);
        if (!$list) {
            $list = self::initModel()->where('level', 1)->field('id,name')->select()->toArray();
            Cache::set(CacheKey::PROVINCE_LIST, json_encode($list));
            return $list;
        }
        return json_decode($list);
    }

    /**
     * 根据省ID获取市
     * @param $provinceId
     * @param $name
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getCityListByProvinceIdService($provinceId, $name)
    {
        $where = [];
        $where[] = ['pid', '=', $provinceId];
        if ($name) {
            $where[] = ['name', 'like', '%' . $name . '%'];
        }
        $list = self::initModel()->where($where)->field('id,name')->select()->toArray();
        return $list;
    }

    /**
     * 根据市ID获取区县
     * @param $cityId
     * @param $name
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getAreaListByCityIdService($cityId, $name)
    {
        $where = [];
        $where[] = ['pid', '=', $cityId];
        if ($name) {
            $where[] = ['name', 'like', '%' . $name . '%'];
        }
        $list = self::initModel()->where($where)->field('id,name')->select()->toArray();
        return $list;
    }

    /**
     * 根据市，区县获取街道
     * @param $cityId
     * @param $areaId
     * @param $operatorId
     * @param $franchiserId
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getStreetListService($cityId, $areaId, $operatorId, $franchiserId)
    {
        $where = [];
        if ($cityId) {
            $cityName = self::initModel()->where('id', $cityId)->value('name');
            $where[] = ['city', '=', $cityName];
        }
        if ($areaId) {
            $areaName = self::initModel()->where('id', $areaId)->value('name');
            $where[] = ['district', '=', $areaName];
        }
        if ($operatorId) {
            $where[] = ['operator_id', '=', $operatorId];
        }
        if ($franchiserId) {
            $where[] = ['franchiser_id', '=', $franchiserId];
        }
        $list = Db::connect('cupboard')
            ->table('street')
            ->field('id,name')
            ->where($where)
            ->whereNull('deleted_at')
            ->order('id', 'desc')
            ->select()
            ->toArray();
        return $list;
    }

    /**
     * 根据街道ID获取小区
     * @param $streetId
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getCommunityListService($streetId)
    {
        $list = Db::connect('cupboard')
            ->table('community')
            ->field('id,name')
            ->where('street_id', $streetId)
            ->whereNull('deleted_at')
            ->order('id', 'desc')
            ->select()
            ->toArray();
        return $list;
    }

    /**
     * 获取省市区地址
     * @param $provinceId
     * @param $cityId
     * @param $areaId
     * @return string
     */
    public static function getAddress(int $provinceId, int $cityId, int $areaId): string
    {
        $or_str = "FIELD(id,$provinceId,$cityId,$areaId)";
        $list = self::initModel()->whereIn('id', [$provinceId, $cityId, $areaId])->orderRaw($or_str)->column('name');
        return implode('', $list);
    }

    /**
     * 获取区域名称
     * @param $id
     * @return mixed
     */
    public static function getRegionName($id)
    {
        $name = self::initModel()->where('id', $id)->value('name');
        return $name;
    }

    /**
     * 根据名称查询ID
     * @param $name
     * @return mixed
     */
    public static function getRegionIdByName($name)
    {
        $id = self::initModel()->where('name', $name)->value('id');
        return $id;
    }
}